PROJECT := sysy
WORK_DIR := $(shell pwd)
BUILD := $(WORK_DIR)/build
OBJS_DIR := $(BUILD)/objs
BINARY := $(BUILD)/compiler

CXX := g++
LD := g++

CXXFLAGS += -Wall -Wextra -MMD -std=c++17
CXXFLAGS += -g -DDEBUG

LDFLAGS +=

INC_PATH += include
CXXFLAGS += $(addprefix -I, $(INC_PATH))

.PHONY: all
all: $(BINARY)

include src/parser/parser.mk
include src/filelist.mk

OBJS = $(SRCS:%.cc=$(OBJS_DIR)/%.o)

$(OBJS_DIR)/%.o: %.cc $(PARSER_CXX)
	@echo + CXX $<
	@mkdir -p $(dir $@)
	@$(CXX) $(CXXFLAGS) -c -o $@ $<

-include $(OBJS:.o=.d)

$(BINARY): $(OBJS)
	@echo + LD $(subst $(WORK_DIR)/,,$@)
	@$(LD) $(LDFLAGS) -o $@ $(OBJS)

.PHONY: release
release: CXXFLAGS := $(filter-out -DDEBUG, $(filter-out -g, $(CXXFLAGS))) -O2
release: all

.PHONY: address
address: CXXFLAGS += -fsanitize=address
address: LDFLAGS += -fsanitize=address
address: all

.PHONY: clang
clang: CXX = clang++
clang: LD = clang++
clang: all

.PHONY: clean
clean:
	-@rm -rf $(BUILD)
	# remove parser
	-@rm $(PARSER_CLEAN_FILES)
